
/** 게시판>게시판 를 호출되어지는 데이터 처리 Controller                                
 * @FileName  : flyController.java 0.1 2014년09월12일(14:05:50)       
 * @Project     : nComz n-eip 프로젝트   
 * @Date         : 2014년09월12일(14:05:50)          
 * @작성자      : 신용천(nComz)           	  
 * @변경이력 :                         
 * @프로그램 설명 :게시판>게시판(Controller)    
 * Copyright 2012 (주)nComz Cop. All rights Reserved.                  
 */ 

package com.sunny.hp.fly; 

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.sunny.hp.common.servlet.ModelAndNCOMZView;
import com.sunny.hp.common.util.HttpRequest;
import com.sunny.hp.fly.service.FlyService;

/** 게시판>게시판 를 호출되어지는 데이터 처리 Controller                               
 * @ClassName  : flyController       
 * @Date         : 2014년09월12일(14:05:50)         
 * @작성자      : 신용천(nComz)           	 
 * @변경이력 :                        
 * @프로그램 설명 :게시판>게시판(Controller)                     
 */      

@Controller
@RequestMapping("/fly")
public class FlyController {

	@Autowired
	private FlyService flyService; 

	protected final Log logger = LogFactory.getLog(getClass());

	/**  비행 정보 선택하기 위한 초기 정보들을 가져온다.					
	 * @Method Name  : selectFlyInitialDataInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 공지사항 게시판                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/selectFlyOptionInfoView.do")
	public ModelAndNCOMZView selectFlyOptionInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request);
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		ArrayList<LinkedHashMap> flyTypeList = new ArrayList<LinkedHashMap>();// 비행기 타입 		
		ArrayList<LinkedHashMap> depAirportList = new ArrayList<LinkedHashMap>();//출발 공항 리스트
		ArrayList<LinkedHashMap> desAirportList = new ArrayList<LinkedHashMap>();//도착 공항 리스트
		try {
			flyTypeList = (ArrayList<LinkedHashMap>)flyService.selectFlyTypeInfoList(param);			
			param.put("airport_depdes_type", "dep");			
			depAirportList = (ArrayList<LinkedHashMap>)flyService.selectAirportInfoList(param);
			param.put("airport_depdes_type", "des");
			desAirportList = (ArrayList<LinkedHashMap>)flyService.selectAirportInfoList(param);
			
			mav.addObject("flyTypeList", flyTypeList); 
			mav.addObject("depAirportList", depAirportList); 
			mav.addObject("desAirportList", desAirportList); 
			
			mav.setViewName("fly/selectFlyOptionInfoView");
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}
	
	/**  비행 정보 선택하기 위한 초기 정보들을 가져온다.					
	 * @Method Name  : selectFlyInitialDataInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 공지사항 게시판                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectFlyTypeInfo.do")
	public ModelAndNCOMZView selectFlyTypeInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		ArrayList<LinkedHashMap> flyTypeList = new ArrayList<LinkedHashMap>();// 비행기 타입 		
		try {
			flyTypeList = (ArrayList<LinkedHashMap>)flyService.selectFlyTypeInfoList(param);			
			
			mav.addObject("flyTypeInfo", flyTypeList.get(0)); 
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}
	
	/**  공항 정보를 가져온다.					
	 * @Method Name  : selectAirportInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 공항 정보를 가져온다.                         		
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectAirportInfo.do")
	public ModelAndNCOMZView selectAirportInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		ArrayList<LinkedHashMap> ariportInfo = new ArrayList<LinkedHashMap>();// 비행기 타입 		
		try {
			ariportInfo = (ArrayList<LinkedHashMap>)flyService.selectAirportInfoList(param);			
			
			mav.addObject("ariportInfo", ariportInfo.get(0)); 
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}
	

	/**  착륙 거리 계산 후 리턴 					
	 * @Method Name  : selectLandingDistanceInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 착륙 거리 계산 후 리턴 		 			
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/ajax/selectLandingDistanceInfo.do")
	public ModelAndNCOMZView selectLandingDistanceInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		
		double press_alt_ = Double.parseDouble(param.get("press_alt")+""); // 높이별 기압
		double temp_ = Double.parseDouble(param.get("temp")+""); // 온도
		
		String distanceType = param.get("distanceType")+"";
		
		double press_alt_last = press_alt_ % 1000;
		double temp_last = temp_ % 10;
		
		ArrayList<LinkedHashMap> distanceList = new ArrayList<LinkedHashMap>();// 비행기 타입 		
		
		double press_alt_1 = press_alt_ - press_alt_%1000; // 이전값(기압)
		double press_alt_2 = press_alt_1 + 1000; // 이후값(기압)
		
		double temp_1 = temp_ - temp_%10; // 이전값(온도)
		double temp_2 = temp_1 + 10; // 이후값(온도)
		

		
		try {
			param.put("press_alt", press_alt_1);
			param.put("temp_1", temp_1);
			param.put("temp_2", temp_2);
			
			if(distanceType.equals("landing")){
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectLandingDistanceInfo(param);
			}else{
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectTakeOffDistanceInfo(param);
			}
			
			double grnd_roll_ft1 = 0;
			double takeoff_distance1 = 0;
			double grnd_roll_ft1_ = 0;
			double takeoff_distance1_ = 0;
			
			for(LinkedHashMap map : distanceList){
				if(temp_1 == Double.parseDouble(map.get("temp")+"")){
					grnd_roll_ft1 = Double.parseDouble(map.get("grnd_roll_ft")+"");
					takeoff_distance1 = Double.parseDouble(map.get("takeoff_distance")+"");
				}
				if(temp_2 == Double.parseDouble(map.get("temp")+"")){
					grnd_roll_ft1_ = Double.parseDouble(map.get("grnd_roll_ft")+"");
					takeoff_distance1_ = Double.parseDouble(map.get("takeoff_distance")+"");
				}
			}
			
			
			param.put("press_alt", press_alt_2);
			if(distanceType.equals("landing")){
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectLandingDistanceInfo(param);
			}else{
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectTakeOffDistanceInfo(param);
			}
			
			double grnd_roll_ft2 = 0;
			double takeoff_distance2 = 0;
			double grnd_roll_ft2_ = 0;
			double takeoff_distance2_ = 0;
			
			for(LinkedHashMap map : distanceList){
				if(temp_1 == Double.parseDouble(map.get("temp")+"")){
					grnd_roll_ft2 = Double.parseDouble(map.get("grnd_roll_ft")+"");
					takeoff_distance2 = Double.parseDouble(map.get("takeoff_distance")+"");
				}
				if(temp_2 == Double.parseDouble(map.get("temp")+"")){
					grnd_roll_ft2_ = Double.parseDouble(map.get("grnd_roll_ft")+"");
					takeoff_distance2_ = Double.parseDouble(map.get("takeoff_distance")+"");
				}
			}
			
			double grnd_roll_ft = 0;
			double takeoff_distance = 0;
			double grnd_roll_ft_ = 0;
			double takeoff_distance_ = 0;
			
			System.out.println("grnd_roll_ft1 == > " +  + grnd_roll_ft1);
			System.out.println("grnd_roll_ft2 == > " +  + grnd_roll_ft2);
			
			System.out.println("takeoff_distance1 == > " +  + takeoff_distance1);
			System.out.println("takeoff_distance2 == > " +  + takeoff_distance2);
			
			System.out.println("grnd_roll_ft1_ == > " +  + grnd_roll_ft1_);
			System.out.println("grnd_roll_ft2_ == > " +  + grnd_roll_ft2_);
			
			System.out.println("takeoff_distance1_ == > " +  + takeoff_distance1_);
			System.out.println("takeoff_distance2_ == > " +  + takeoff_distance2_);
			
			if(grnd_roll_ft2 != 0){
				grnd_roll_ft = (grnd_roll_ft2 - grnd_roll_ft1)*press_alt_last/1000 + grnd_roll_ft1;
			}
			System.out.println("==============================================================");
			System.out.println("grnd_roll_ft == > " +  + grnd_roll_ft);
			if(takeoff_distance2 != 0){
				takeoff_distance = (takeoff_distance2 - takeoff_distance1)*press_alt_last/1000 + takeoff_distance1;
			}
			System.out.println("==============================================================");
			System.out.println("takeoff_distance == > " +  + takeoff_distance);
			
			if(grnd_roll_ft2_ != 0){
				grnd_roll_ft_ = (grnd_roll_ft2_ - grnd_roll_ft1_)*press_alt_last/1000 + grnd_roll_ft1_;
			}
			
			System.out.println("==============================================================");
			System.out.println("grnd_roll_ft_ == > " +  + grnd_roll_ft_);
			
			if(takeoff_distance2_ != 0){
				takeoff_distance_ = (takeoff_distance2_ - takeoff_distance1_)*press_alt_last/1000 + takeoff_distance1_;
			}
			
			System.out.println("==============================================================");
			System.out.println("takeoff_distance_ == > " +  + takeoff_distance_);
			
//			System.out.println("grnd_roll_ft1_ == > " +  + grnd_roll_ft1_);
//			System.out.println("grnd_roll_ft2_ == > " +  + grnd_roll_ft2_);
//			
//			System.out.println("takeoff_distance1_ == > " +  + takeoff_distance1_);
//			System.out.println("takeoff_distance2_ == > " +  + takeoff_distance2_);
			
			
			double grnd_roll_ft_org = 0;
			double takeoff_distance_org = 0;	
			
			
			grnd_roll_ft_org = (grnd_roll_ft_ - grnd_roll_ft)*temp_last/10 + grnd_roll_ft;			
			takeoff_distance_org = (takeoff_distance_ - takeoff_distance)*temp_last/10 + takeoff_distance;
			
			mav.addObject("ground_roll", grnd_roll_ft_org); 
			mav.addObject("takeoff_distance", takeoff_distance_org); 
			
			System.out.println(" grnd_roll_ft_org : "  + grnd_roll_ft_org);
			System.out.println(" takeoff_distance_org : "  + takeoff_distance_org);
			
			//maximum rate-of-climb			
			if(distanceType.equals("takeoff")){
				press_alt_last = press_alt_ % 2000;
				temp_last = temp_ % 20;
				
				press_alt_1 = press_alt_ - press_alt_%2000; // 이전값(기압)
				press_alt_2 = press_alt_1 + 2000; // 이후값(기압)
				
				temp_1 = temp_ - temp_%20; // 이전값(온도)
				temp_2 = temp_1 + 20; // 이후값(온도)
				
				param.put("press_alt", press_alt_1);
				param.put("temp_1", temp_1);
				param.put("temp_2", temp_2);
				
			
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectMakOfClimbInfo(param);
				
				double fpm1 = 0;
				double fpm1_ = 0;
			
				for(LinkedHashMap map : distanceList){
					if(temp_1 == Double.parseDouble(map.get("temp")+"")){
						fpm1 = Double.parseDouble(map.get("fpm")+"");
					}
					if(temp_2 == Double.parseDouble(map.get("temp")+"")){
						fpm1_ = Double.parseDouble(map.get("fpm")+"");
					}
				}
				
				param.put("press_alt", press_alt_2);
				distanceList = (ArrayList<LinkedHashMap>)flyService.selectMakOfClimbInfo(param);
				double fpm2 = 0;
				double fpm2_ = 0;
			
				for(LinkedHashMap map : distanceList){
					if(temp_1 == Double.parseDouble(map.get("temp")+"")){
						fpm2 = Double.parseDouble(map.get("fpm")+"");
					}
					if(temp_2 == Double.parseDouble(map.get("temp")+"")){
						fpm2_ = Double.parseDouble(map.get("fpm")+"");
					}
				}
				
				double fpm = 0;
				double fpm_ = 0;
				
				
				
				if(fpm2 != 0){
					fpm = fpm1 - (fpm1 - fpm2)*press_alt_last/2000;
				}
				if(fpm2_ != 0){
					fpm_ = fpm1_ - (fpm1_ - fpm2_)*press_alt_last/2000;
				}
				
				double fpm_org = 0;
				fpm_org = fpm - (fpm - fpm_)*temp_last/20;		
				
				mav.addObject("rate_of_climb", fpm_org); 

			}
			
			
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}
	
	/**  이륙 거리 계산 후 리턴 					
	 * @Method Name  : selectTakeOffDistanceInfo   		
	 * @작성일   : 2014년09월12일(14:05:50)                    		
	 * @작성자   : 신용천(nComz)                    		
	 * @변경이력  :                           		
	 * @Method 설명 : 이륙 거리 계산 후 리턴 		 			
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return mav                                	
	 */
	@RequestMapping(value="/selectTakeOffDistanceInfo.do")
	public ModelAndNCOMZView selectTakeOffDistanceInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndNCOMZView mav   	= new ModelAndNCOMZView(request,new MappingJacksonJsonView());
		HttpRequest requestUtil = new HttpRequest(request); 
		LinkedHashMap param = requestUtil.paramBind(); 
		ArrayList<LinkedHashMap> flyTypeList = new ArrayList<LinkedHashMap>();// 비행기 타입 		
		try {
			flyTypeList = (ArrayList<LinkedHashMap>)flyService.selectFlyTypeInfoList(param);			
			param.put("airport_depdes_type", "dep");			
			
			mav.addObject("flyTypeList", flyTypeList); 
			
		} catch (Exception e) {
			e.printStackTrace(); 
		} 

		return mav;
	}
	


}
